﻿
package com.webbei.Backstage.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.taobao.api.domain.Shipping;
import com.wabbei.fenxiao.action.MyTaobaoUtil;
import com.wabbei.fenxiao.action.data;
import com.webbei.Backstage.service.BackstateSer;

public class UpdateOrderstate extends ActionSupport{
      private BackstateSer bao;
	  private String orderid;
	  private String orderNumber;
      private String out_sid;
      private String msg;
      private String company_code;

	  public String execute(){
		  System.out.println("company_code:"+company_code);
		  MyTaobaoUtil taoUtil = new MyTaobaoUtil();
		  Long id=Long.parseLong(orderid);
		  if(orderid.length()==16){
			  String trade_no = orderid;
			  String invoice_no = out_sid;
			  String logistics_name=company_code;
			  String transport_type="";
			  if("POST".equals(company_code)||"EMS".equals(company_code)){
				  transport_type=company_code;
			  }else{
				  transport_type="EXPRESS";
			  }
			  Boolean b = taoUtil.alipaySendOut(trade_no, logistics_name, invoice_no, transport_type);
			  if(b==true){
				  msg="success";
				  bao.updateOrderstate(id,out_sid);
			  }else{
				  msg="发货失败";
			  }
		  }else{
			  String seller = bao.getOrdersByTradeId(Long.parseLong(orderid.trim())).get(0).getSellername();
			  Map<String,Object> m = null;
			  if("代洋洋816".equals(seller)){
				  m =  MyTaobaoUtil.confirmLogistics(id, out_sid,company_code,data.sessionKey2);
			  }
			  if("网贝旗舰店".equals(seller)){
				  m =  MyTaobaoUtil.confirmLogistics(id, out_sid,company_code,data.sessionKey1);
			  }
			  Shipping ship = (Shipping) m.get("ship");
			  
			 if(ship!=null){
				 if(ship.getIsSuccess()==true){
				 int i = bao.updateOrderstate(id,out_sid);
				  msg = "success";
				 }
			  }else{
				  msg = (String) m.get("msg");
				  if(msg==null){
					  msg="发货失败";
				  }
			  }
		  }
		  //bao.updateStockout(id,"");
		  return SUCCESS;
	  }

	public void setBao(BackstateSer bao) {
		this.bao = bao;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getOrderNumber() {
		return orderNumber;
	}

	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}

	public String getOut_sid() {
		return out_sid;
	}

	public void setOut_sid(String outSid) {
		out_sid = outSid;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCompany_code() {
		return company_code;
	}

	public void setCompany_code(String companyCode) {
		company_code = companyCode;
	}
	  
	  
}
